<?php
// Load the framework
include('includes/base.inc');

#### INDEX CLASS ###############################################################
class Index {

	var $output;
	var $page_title;

	#### CONSTRUCTOR ###########################################################
	function Index() {
		Base::getThis();

		// Start the clock!
		$this->load->helper('date');
		$this->var['generation_time'] = micro_time();

		// Include common helpers and libraries
		$this->load->helper('common');
		$this->load->helper('url');
		$this->load->library('template', TRUE);
		$this->load->helper('method');
		$this->load->helper('bbcode');
		$this->load->helper('smilies');
		$this->load->helper('messaging');
		$this->load->helper('locale');

		// Create a new database connection and make database functions
		// available under $this->db
		$this->db = $this->load->library('database');
		$this->db->initialise();

		// Grab common and config data e.g $this->config['name']
		$this->db->getConfigTable();
		$this->db->getCommonTable();

		// Set default site locale
		locale('default', $this->config['locale']);

		$this->load->helper('permissions'); // We want to use groups so this must be included before the session library
		$this->session = $this->load->library('session'); // Start new session and make it available under $this->sessvar

		// Include icons
		$this->load->file('includes/config/icons.inc');
		$this->icon = get_icons();

		// Define common variables
		variable($this->post, 'post', 'post');
		variable($this->submit, 'submit', 'post');
		variable($this->file, 'file', 'files');
		variable($this->page, 'page', 'get' , url('page'));
		variable($this->step, 'step', 'get' ,url('step'));
		variable($this->confirm, 'confirm', 'get', url('search', 'confirm'));

		$url_action_1 = clean_method_url(url(1));
		$url_action_2 = clean_method_url(url(2));

		// Define the module name
		if ($url_action_1 == 'manage' && $url_action_2) $this->var['module_name'] = $url_action_2;
		else $this->var['module_name'] = $url_action_1;

		// Checks session is valid on every page load and will automatically log
		// a user in if a valid cookie is found
		$this->session->check(TRUE);

		// Show disabled message if needed
		if ($this->config['site_closed']) {
			$this->output = '<div class="box">'.$this->config['site_disabled_message'].'</div>';
			die($this->output());
		}

		// If the action method exists in this class call it, else call the default action home
		if (method_exists($this, $url_action_1)) $this->{$url_action_1}();
		else if ($url_action_1) $this->module();
		else $this->home();

		// Okay, we're all done, close the database connection
		$this->db->close();
	}

	#### MODULE ################################################################
	function module() {
		// Include module controller and grab its output
		$module = $this->load->module(url(1));
		if ($module == FALSE) return $this->home();

		// Get the modules output
		$this->output = $module->output;

		// Set the page title
		$this->page_title = $module->page_title;
	}

	#### MANAGE ################################################################
	function manage() {
		// Needed to ensure fresh sessvar
		Base::getThis();

		// If anonymous, ask user to login
		if (!$this->sessvar['login']) {
			return $this->login();
			die(); // Precautions are advisable kids
		}

		// Generate the managmement header
		$this->load->file('modules/management/management_header.inc');
		$this->output = management_header();

		// Include module controller and grab its output else include default
		// management module
		if (url(2)) $module = $this->load->managementModule(url(2));
		else $module = $this->load->managementModule('management');

		// Get the modules output
		$this->output .= $module->output;

		// Set the page title
		$this->page_title = $module->page_title;
	}

	#### HOME ##################################################################
	function home() {
		$this->pages = $this->load->model('pages');
		$page = $this->pages->getPageByID(1);

		$this->output = box($page['title'], nl2br($page['body']));
	}

	#### LOGIN #################################################################
	function login() {
		// Validation
		if ($this->submit) {
			if (empty($this->post['password'])) $feedback = notice(_('Aren\'t you forgetting your password?'), 'error');
			if (empty($this->post['username'])) $feedback = notice(_('You forgot to enter your username'), 'error');
		} else $feedback = NULL;

		if ($this->post['username'] && $this->post['password']) {
			$this->session->username = $this->post['username'];
			$this->session->password = $this->post['password'];
			$this->session->remember = $this->post['remember'];
			if ($this->session->login()) return $this->home();
			$feedback = notice(_('Oops, looks like you\'ve entered the wrong things!'), 'error');
		}

		$form = $this->load->library('form');

		$form->validate('username', 'alnum');
		$form->validate('password', 'alnum');

		$form->open();
		$form->text(_('Username'), 'username');
		$form->password(_('Password'), 'password');
		$form->checkbox(_('Remember Me'), 'remember');
		$form->button('submit', _('Submit'));
		$form->close();

		$this->output = box(_('Login'), $feedback.$form->output());
	}

	#### POPUP LOGIN ###########################################################
	function popupLogin() {
		$this->load->helper('login');
		popupLoginForm(url(1, 2).'/popup-login');
	}

	#### LOGOUT ################################################################
	function logout() {
		$this->session->logout();
		$this->home();

		if (url('search', 'message')) {
			echo '<h2>'.('Bye bye!').'</h2>
				<hr />';
			echo notice(_('You\'ve been logged out'));
			die();
		}

		// Popup
		$popup = $this->load->library('popup');
		$popup->url('logout/message');
		$popup->width('400px');
		$popup->fade(2000);
		$this->output .= $popup->output();

		$this->output .= redirect($this->config['url'], TRUE, 2500);
	}

	#### OUTPUT ################################################################
	function output() {
		// Needed to ensure fresh sessvar
		Base::getThis();

		// Popup Login
		$popup = $this->load->library('popup');
		$popup->onClickElement('#popup-login');
		$popup->url('popup-login');
		$popup->width('400px');
		$this->output .= $popup->output();

		//$this->load->file('modules/navigation/navigation.inc');
		include('includes/config/javascript.inc');
		include('includes/config/meta.inc');

		$navigation = build_navigation();

		// BE CAREFUL: Only load the debug helper after all queries have run
		if ($this->config['debug_enabled'] && permission('debug')) {
			$this->load->helper('debug');
		 	$debug = debug();
		 } else $debug = NULL;


		$title = (isset($this->page_title) ? $this->page_title : $this->config['slogan']).' - ';
		$body_left = '<div class="widget"><div class="left"><div class="title">'._('Navigation').'</div><ul>'.$navigation.'</ul></div></div>';

		$template = new Template('index');
		$template->assign('TITLE', $title);
		$template->assign('SLOGAN', $this->config['slogan']);
		$template->assign('NAVIGATION', ($this->sessvar['login']['username'] ? _('Welcome').' '.ucwords($this->sessvar['login']['username']).'!' : '<a id="popup-login" href="login">'._('Login').'</a>'));
		$template->assign('KEYWORDS', 'Keywords');
		$template->assign('DESCRIPTION', 'Description');
		$template->assign('GENERATOR', 'Logicfury');
		$template->assign('META', $this->meta);
		$template->assign('JAVASCRIPT', $this->javascript);
		$template->assign('BODY', $this->output);
		$template->assign('RIGHT', NULL);
		$template->assign('LEFT', $body_left);
		$template->assign('TOP', NULL);
		$template->assign('BOTTOM', NULL);
		$template->assign('FOOTER', bb2html(smiley2img(nl2br($this->config['custom_footer']))));
		$template->assign('DEBUG', $debug);

		// Including theme.inc allows us to override template variables and include custom ones
		include('templates/themes/'.$this->config['theme'].'/theme.inc');

		echo $template->output();
	}

}

// Right... lets get this show on the road
$index = new Index();
$index->output();

?>